from model.db import reservoir_sensitivity
from model.db.user_model import TbProject, TbUsers
from model.params.users_params import ProjectParam


def add_project(info: ProjectParam):
    """
       新增
    """
    newProject = TbProject(
        project_name=info.project_name,
        create_user=info.create_user,
        remark=info.remark
    )
    reservoir_sensitivity.add(newProject)
    reservoir_sensitivity.commit()
    return newProject


def get_project(user_id):
    """
       获取自己项目列表
    """
    projs = reservoir_sensitivity.query(TbProject).filter(TbProject.create_user == user_id).all()

    return projs


def get_project_by_id(proj_id):
    """
       获取项目
    """
    proj = reservoir_sensitivity.query(TbProject).filter(TbProject.project_id == proj_id).first()

    return proj


def get_share_project():
    """
       获取共享项目列表
    """
    projs = reservoir_sensitivity.query(TbProject.project_id, TbProject.project_name, TbUsers.user_name,
                                        TbProject.created_time, TbProject.remark).join(TbProject,
                                        TbProject.create_user == TbUsers.user_id).filter(
        TbProject.is_share == 1).all()

    return projs


def delete_project(project_id):
    """
       删除项目
    """
    projs = reservoir_sensitivity.query(TbProject).filter(TbProject.project_id == project_id).delete(
        synchronize_session=False)
    reservoir_sensitivity.commit()
    return projs


def update_project(project_id, project_name, is_share, remark):
    """
       更新项目
    """
    projs = reservoir_sensitivity.query(TbProject).filter(TbProject.project_id == project_id).update(
        {
            'project_name': project_name,
            'is_share': is_share,
            'remark': remark
        }
    )
    reservoir_sensitivity.commit()
    return projs
